package com.chzu.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chzu.mapper.PaperInfoMapper;
import com.chzu.pojo.PaperInfo;
import com.chzu.pojo.vo.ExamHistoryVo;
import com.chzu.pojo.vo.PaperAndAllQuestionVo;
import com.chzu.result.JsonResult;
import com.chzu.result.PageResult;
import com.chzu.result.QueryInfo;
import com.chzu.service.PaperInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName PaperInfoServiceImpl
 * @Description TODO
 * @Date 2022/02/22 11:00
 */
@Service
public class PaperInfoServiceImpl extends ServiceImpl<PaperInfoMapper, PaperInfo> implements PaperInfoService {

    @Resource
    private PaperInfoMapper paperInfoMapper;


    /**
     *  分页查询所有试卷信息
     * @param queryInfo 模糊查询
     * @return
     */
    @Override
    public JsonResult<Object> getAllPaperInfo(QueryInfo queryInfo) {
        PageHelper.startPage(queryInfo.getPageNumber(),queryInfo.getPageSize());
        Page<ExamHistoryVo> page = paperInfoMapper.getAllPaperInfo(queryInfo.getQueryString());
        long total = page.getTotal();
        List<ExamHistoryVo> result = page.getResult();
        return PageResult.pageResult(result, total);
    }

    /**
     * 获取一张试卷中所有试题以及对应的选项
     * @return
     */
    @Override
    public List<Map<String, Object>> getAllQuestionsAndChose() {
        List<PaperAndAllQuestionVo> list = paperInfoMapper.getAllQuestionsAndChose();
        List<Map<String, Object>> collection = new ArrayList<>();
        for(PaperAndAllQuestionVo vo : list) {
            Map<String, Object> map = new HashMap<>(1);
            map.put("paperName", vo.getPaperName());
            map.put("paperId", vo.getPaperId());
            map.put("paperTime", vo.getPaperTime());
            map.put("question", vo.getQuestion());
            map.put("answer", vo.getAnswer());
            String str = vo.getAllChose();
            String[] split = str.split(";");
            List<String> lies = new ArrayList<>();
            for(String cho : split) {
                String chose = cho.substring(0, 1);
                String question = cho.substring(1, cho.length());
                lies.add(chose + ".   " + question);
            }
            map.put("allChose", lies);
            collection.add(map);
        }
        return collection;
    }
}
